package com.mindao.app.weixin.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.jfinal.weixin.sdk.api.ApiResult;
import com.mindao.app.common.spring.service.BaseService;
import com.mindao.app.weixin.model.WxmpUserInfo;

/**
/* 版权所有： 广州敏道科技有限公司
/*
/* 功能描述：
/*
/* 创 建 人：李国才
/* 创建时间：2016年6月26日 上午9:32:15 	
 **/
@Component ("wxmpUserInfoService")
public class WxmpUserInfoServiceImpl implements WxmpUserInfoService {
	private final Logger log = LoggerFactory.getLogger(WxmpUserInfoServiceImpl.class);
	@javax.annotation.Resource(name = "baseService")
	private BaseService baseService;
	
	@Override
	public WxmpUserInfo getUserInfoByOpenid(String openid) {
		// TODO Auto-generated method stub
		List<WxmpUserInfo> uList= baseService.find("from WxmpUserInfo t where t.openid=?",openid);
		return uList.isEmpty()?null:uList.get(0);
	}

	@Override
	public String getUserNicknameByOpenid(String openid) {
		// TODO Auto-generated method stub
		List<String> uList= baseService.find("select t.nickname from WxmpUserInfo t where t.openid=?",openid);
		return uList.isEmpty()?null:uList.get(0);
	}

	@Override
	public WxmpUserInfo updateUserInfo(String openid, ApiResult ar) {
		// TODO Auto-generated method stub
		WxmpUserInfo info= getUserInfoByOpenid(  openid);
		if (info==null){
			info=new WxmpUserInfo();
			info.setOpenid(openid);
		}
		//用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息。
		Integer subscribe =ar.getInt("subscribe");
		if (subscribe!=null && subscribe.intValue()==1){
			info.setSubscribe(subscribe);
			info.setNickname(ar.getStr("nickname"));
			info.setSex(ar.getInt("sex"));
			info.setCity(ar.getStr("city"));
			info.setCountry(ar.getStr("country"));
			info.setProvince(ar.getStr("province"));
			info.setLanguage(ar.getStr("language"));
			info.setHeadimgurl(ar.getStr("headimgurl"));
			
			info.setSubscribe_time(ar.getInt("subscribe_time"));
			info.setUnionid(ar.getStr("unionid"));
			info.setRemark(ar.getStr("remark"));
			info.setGroupid(ar.getInt("groupid"));
			List list=ar.getList("tagid_list");
			String listStr=list.toString();
			log.info("tagid_list"+listStr);
			info.setTagid_list(listStr);
			info.setSubscribe(subscribe);
		}else{//oauth过来的用户
			info.setNickname(ar.getStr("nickname"));
			info.setSex(ar.getInt("sex"));
			info.setCity(ar.getStr("city"));
			info.setCountry(ar.getStr("country"));
			info.setProvince(ar.getStr("province"));
			info.setLanguage(ar.getStr("language"));
			info.setHeadimgurl(ar.getStr("headimgurl"));
		}
		
		if (info.getId()==null){
			baseService.create(info);
		}else{
			baseService.update(info);
		}
		
		return info;
	}
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();  
		list.add("str1");  
		list.add("str2");  
		int size = list.size();  
		Object[] arr = list.toArray();
		//String[] arr = (String[])list.toArray(new String[size]);//使用了第二种接口，返回值和参数均为结果
		System.out.println(list.toString());
		
	}
}
